package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

class LoginController {
    // shared methods
    def sendResponse(statusCode) {
//        if (statusCode == 404) {
//            JSONObject o = new JSONObject()
//            o.put(statusCode, true)
//            println o
//            render o as JSON
//        }
        
        response.status = statusCode
        println response
        render response
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    
    

    // REST operations
    // GET
    
    // PUT
    def update = {
        // println 'LoginController >> Show >> START'
        JSONObject o = request.JSON
        
        CustomerManagement cman = new CustomerManagement()
        
        if (o.has("username") && o.has("password")) {
            def username = o.get("username")
            def password = o.get("password")
            
            def c = null
            c = cman.getCustomerByUsername(username)
            println c
            if (c.is(null)) {
                println "404-1"
                sendResponse(404)
            }
            else {
                if (c.activationStatus == true) {
                    if (c.password == password) {
                        println 'Show login by USERNAME >> ' + c.id + " " + c
                        cman.updateLastLogin(c)   
                        render c as JSON
                        println "200"
                        sendResponse(200)
                    }
                    else {
//                        render '403' as JSON
                        println "403"
                        sendResponse(403)
                    }
                }
                else {
//                    render '404' as JSON
                    println "404"
                    sendResponse(404)
                }
            }
        }
        else {
            println "400"
            sendResponse(400)
        }
        
        // println 'LoginController >> Show >> END'
    }
    
    def updateResetPassword = {
        // println 'LoginController >> Show >> START'
        JSONObject o = request.JSON
        
        CustomerManagement cman = new CustomerManagement()
        
        if (o.has("username") && o.has("email")) {
            def username = o.get("username")
            def email = o.get("email")
            
            def c = null
            c = cman.getCustomerByUsername(username)
            println c
            if (c.is(null)) {
                println "404-1"
                sendResponse(404)
            }
            else {
                if (c.activationStatus == true) {
                    if (c.email == email) {
                        println 'Show login by FORGOT PW >> ' + c.id + " " + c
                        cman.updatePassword(c)   
                        render c as JSON
                        println "200"
                        sendResponse(200)
                    }
                    else {
//                        render '403' as JSON
                        println "403"
                        sendResponse(403)
                    }
                }
                else {
//                    render '404' as JSON
                    println "404"
                    sendResponse(404)
                }
            }
        }
        else {
            println "400"
            sendResponse(400)
        }
        
        // println 'LoginController >> Show >> END'
    }
    
    
    // DELETE
    
    
    // POST
    
}
